package com.tc.zigbee.entity.device;

import java.util.List;

import com.tc.gateway.device.AbstractDevice;
import com.tc.gateway.device.listener.GeneralDeviceListener;
import com.tc.util.DataUtil;
import com.tc.zigbee.entity.Endpoint;
import com.tc.zigbee.mt.MtCommand;
import com.tc.zigbee.mt.ZigbeeManager;
import com.tc.zigbee.mt.request.RequestFactory;
import com.tc.zigbee.zcl.ZclCommandCreator;
import com.tc.zigbee.zcl.ZclConstants;

abstract public class ZigbeeDevice<T extends GeneralDeviceListener> extends AbstractDevice<T>{

	public String description() {
		// TODO Auto-generated method stub
		return endpoint.description();
	}

	protected Endpoint endpoint;
	protected ZigbeeManager manager;
	public ZigbeeDevice(Endpoint endpoint, ZigbeeManager m) {
		super();
		this.manager = m;
		this.endpoint = endpoint;
	}
	
	public Endpoint getEndpoint() {
		return endpoint;
	}

	@Override
	public String getId() {
		return DataUtil.hex(this.endpoint.getParent().getIeeeAddress()) + DataUtil.hex(endpoint.getEndpointId());

	}

	@Override
	public String getType() {
		return "d" + DataUtil.hex(this.endpoint.getProfileId()) + DataUtil.hex(endpoint.getDeviceId());

	}

	

	public void read(List<Short> attribs){
		byte[] zclcommand = ZclCommandCreator.read((byte)0, attribs);
		MtCommand mc = RequestFactory.MtAfDataRequest((byte)2,
				this.endpoint.getParent().getNwkAddress(), 
				//(short)1,
				endpoint.getEndpointId(), 
				(byte)manager.remoteControl.getEndpointId(), 
				ZclConstants.ZCL_CLUSTER_ID_GEN_LEVEL_CONTROL, 
				(byte)0, 
				(byte)0, 
				(byte)2, 
				(byte)zclcommand.length, 
				zclcommand
				);
	}

	public Object getAttribute(short cluster, short attr) {
		// TODO Auto-generated method stub
		return endpoint.getAttribute(cluster, attr);
	}

	@Override
	public String toString() {
		if(this.getName() != null)
			return this.getName();

		return endpoint.toString();
	}
	
}
